<?php

/**
 * 产品管理
 * @author zlj <389843954@qq.com>
 */

namespace app\manage\controller;
use app\common\model\Product_info as ProductModel;
class Product extends Common
{
	/**
	 * 产品列表
	 * @return [type] [description]
	 */
	public function plists(){
		$keyword = input('keyword');

		$productModel = new ProductModel();
		$order = "time desc";
		$pageParam = array();
		$where = array();
        if ($keyword) {
            $pageParam['query']['keyword'] = $keyword;
            $where = [
                'name' => ['like', '%' . $keyword . '%']
            ];
        }
		$data = $productModel->getAll($where,$order,15,$pageParam);

		$res = [
			'keyword'=>$keyword,
			'data'=>$data,
			'title'=>'产品列表'
		];

		return view('',$res);
	}

	/**
	 * 产品添加/修改操作
	 * @return [type] [description]
	 */
	public function padds(){
		if(request()->isPost()){
            $uid = session('user.id');
			$name = input('post.name');
			$type = input('post.type');
			$standard = input('post.standard');
			$price = input('post.price');

			if(!$name || !$type || !$standard || !$price ){
				$this->error('信息填写不完善');
			}

			$productModel = new ProductModel();
			$data = [
				'name'=>$name,
				'type'=>$type,
				'standard'=>$standard,
				'price'=>$price,
				'uid'=>$uid,
				'time'=>time()
			];
			$res = $productModel->save($data);
			if($res){
				$this->redirect('Product/plists');
			}else{
				$this->error('上传失败,请重试!');
			}

		}

		return view('',['title'=>'产品添加']);
	}

	/**
	 * 删除产品
	 * @return [type] [description]
	 */
	public function delpro(){
		if(request()->isAjax()){
			$pid = input('id');
			$ProductModel = new ProductModel();
			$ProductModel->updInfo(['id'=>$pid],['valid'=>0]);
		}
	}

	/**
	 * 修改操作
	 * @return [type] [description]
	 */
	public function pupd(){
		$productModel = new ProductModel();

		if(request()->isPost()){

            $uid = session('user.id');
			$name = input('post.name');
			$type = input('post.type');
			$standard = input('post.standard');
			$price = input('post.price');
			$pid = input('post.pid');

			if(!$name || !$type || !$standard || !$price ){
				$this->error('信息填写不完善');
			}

			$data['name'] = $name;
			$data['type'] = $type;
			$data['standard'] = $standard;
			$data['price'] = $price;
			$data['uid'] = $uid;

			$res = $productModel->save($data,['id'=>$pid]);
			if($res){
				$this->redirect('Product/plists');
			}else{
				$this->error('上传失败,请重试!');
			}

		}else{
			$pid = input('pid');
			$infos = $productModel->getOne(['id'=>$pid]);
			return view('',['title'=>'产品修改','infos'=>$infos,'pid'=>$pid]);
		}
	}
	
	//征询意见
	public function inquiry(){
		return view();
	}
	
	//抄送界面
	public function copy(){
		return view();
	}
	
	//重修
	public function rework(){
		return view();
	}
	
	//返修
	public function modify(){
		return view();
	}
}